package tech.aistar.dao.impl;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import tech.aistar.dao.ICustomerDao;
import tech.aistar.entity.Customer;
import tech.aistar.until.MybatisUtil;

import java.io.InputStream;
import java.util.List;

/**
 * @author Ray
 * @version 1.0
 * @description:本类用来演示:
 * @date 2019/8/21  12:33
 */
public class CustomerDaoImpl implements ICustomerDao {
    @Override
    public List<Customer> findAll() {
        String path = "mybatis-config.xml";

        InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
//        System.out.println(in);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<Customer> customers = sqlSession.selectList("success.findAll");
        sqlSession.close();
        return customers;
    }

    @Override
    public void delById(Integer id) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        sqlSession.delete("success.delById",id);
        sqlSession.commit();
        sqlSession.close();
    }
}
